package com.iflytek.day23;

import java.util.HashSet;
import java.util.Objects;

/**
 * 学生类
 */
class Student {
  private String name;
  private int age;

  public Student() {
  }

  public Student(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Student student = (Student) o;
    return age == student.age && Objects.equals(name, student.name);
  }

  @Override
  public int hashCode() {
    return Objects.hash(name, age);
  }
}

/**
 * 测试类
 */
public class _02_HashSetDemo {
  public static void main(String[] args) {
    // 创建HashSet集合对象
    HashSet<Student> hs = new HashSet<Student>();

    // 创建学生对象
    Student s1 = new Student("林青霞", 30);
    Student s2 = new Student("张曼玉", 35);
    Student s3 = new Student("王祖贤", 33);

    Student s4 = new Student("王祖贤", 33);

    // 把学生添加到集合
    hs.add(s1);
    hs.add(s2);
    hs.add(s3);
    hs.add(s4);

    // 遍历集合(增强for)
    for (Student s : hs) {
      System.out.println(s.getName() + "," + s.getAge());
    }
  }
}
